package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.server.PaymentHyStrixServer;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "global_fallback")//定义全局服务降级
public class PaymentController {

    @Resource
    private PaymentHyStrixServer paymentHyStrixServer;


    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    String payment_OK(@PathVariable("id") Integer id){
        return paymentHyStrixServer.payment_OK(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    //指定的服务降级
//    @HystrixCommand(fallbackMethod = "payment_timeout2", commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "1000")
//    })
    //全局的服务降级
    @HystrixCommand
    public String payment_timeout(@PathVariable("id") Integer id){
        return paymentHyStrixServer.payment_timeout(id);


    }

    public String payment_timeout2(@PathVariable("id") Integer id){
        return "服务降级80";
    }


    public String global_fallback(){
       return "全局服务降级";
    }
}
